package com.coldchain.cys.api;

import cn.hutool.core.util.StrUtil;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.constant.PathUtils;
import com.coldchain.cys.dev.ImageService;
import com.coldchain.cys.mapping.ApiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(description="文件上传controller")
@RestController
public class FileController {
	
	@ApiOperation(value="文件上传")
	@ApiImplicitParams(value={
			@ApiImplicitParam(name="file",value="文件",dataType="File",paramType="formData"),
			@ApiImplicitParam(name="modelType",value="模块名称（例如司机填carDriver，车辆填car）",dataType="String",paramType="formData"),
		})
	@RequestMapping(value=ApiUtils.UPLOAD_FILE,method= {RequestMethod.POST})
	public ObjectRestResponse<String> uploadFile(MultipartFile file, String modelType) throws IOException{
		String path = "" ; 
		if(StrUtil.isBlank(modelType)) {
			return (ObjectRestResponse<String>) new ObjectRestResponse<String>().error("参数错误") ;
		}
		if(modelType.equals("carDriver")) {
			path = PathUtils.car_driver_image ;
		}
		if(modelType.equals("car")) {
			path = PathUtils.car_image ;
		}
		if(StrUtil.isBlank(path)) {
			return (ObjectRestResponse<String>) new ObjectRestResponse<String>().error("参数错误") ;
		}
		String filePath = ImageService.upload(file.getOriginalFilename(), file.getInputStream(), path);
		return new ObjectRestResponse<String>(filePath) ;
	}
}
